#include "shop_1.h"
#include "ui_shop_1.h"
#include <QBuffer>
#include <QImage>
#include <QFile>
#include <QFileDialog>

Shop_1::Shop_1(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Shop_1)
{
    ui->setupUi(this);

    m_status = new QLabel(this);
    m_sxt = new mysxt;

    setWindowTitle("周同学半岛盒饭");

    m_tcpSock = new QTcpSocket(this);

    ui->bt_2->setDisabled(false);
    ui->bt_3->setDisabled(false);


    // 创建客户端的套接字对象并连接

    connect(m_tcpSock, &QTcpSocket::connected, this, [=]()
    {
        QPixmap pix(":/res/on.ico");
        pix = pix.scaled(20,20);
        m_status->setPixmap(pix);

        ui->bt_7->setEnabled(false);
        m_tcpSock->write("您有新的订单，请及时处理");

    });
    connect(m_tcpSock, &QTcpSocket::readyRead, this, [=]()
    {
        QByteArray ba = m_tcpSock->readAll();

        ui->te_1->append(ba);
    });

}

void Shop_1::recvStr(QString str)
{
    m_str = str;
}

void Shop_1::recvInt(int num)
{
    m_num = num;
}

void Shop_1::recvInt1(int num)
{
    m_num1 = num;
    qDebug() << "m_num1:" << m_num1;
}

void Shop_1::recvInt2(int num)
{
    m_num2 = num;
    qDebug() << "m_num2:" << m_num2;
}

void Shop_1::recvInt3(int num)
{
    m_num3 = num;
    qDebug() << "m_num3:" << m_num3;
}

Shop_1::~Shop_1()
{
    delete ui;
}

void Shop_1::on_bt_1_clicked()//发送消息
{

    while (i--)
    {
        QString str = QString("客户地址: %1 \n").arg(m_str);
        m_tcpSock->write(str.toUtf8());
        if(m_num1 != 0)
        {
            QString str = QString("客户点餐: %1 份脆皮鸡土豆牛腩饭\n").arg(QString::number(m_num1));
            m_tcpSock->write(str.toUtf8());
        }
        if(m_num2 != 0)
        {
            QString str = QString("客户点餐: %1 份脆皮鸡孜然肉片饭\n").arg(QString::number(m_num2));
            m_tcpSock->write(str.toUtf8());
        }
        if(m_num3 != 0)
        {
            QString str = QString("客户点餐: %1 份脆皮鸡红烧茄子饭\n").arg(QString::number(m_num3));
            m_tcpSock->write(str.toUtf8());
        }
        QString str1 = QString("客户共消费 %1 元\n").arg(QString::number(m_num));
        m_tcpSock->write(str1.toUtf8());
    }

    QString msg = "客户:" + ui->te_2->toPlainText();
    m_tcpSock->write(msg.toUtf8());

    ui->te_1->append(msg);
    ui->te_2->clear();
}

void Shop_1::on_bt_2_clicked()//发送图片
{
    QString str = QFileDialog::getOpenFileName(this, "选择文件","d:/",
                                        "img(*.ico *.jpg *.png)");
    QImage image(str); // 从文件加载图片
    if (image.isNull())
    {
        qWarning() << "无法加载图片:" << str;
        return;
    }

    QByteArray imageData; // 用于存储图片数据的字节数组
    QBuffer buffer(&imageData); // 使用QBuffer将QByteArray封装为QIODevice
    buffer.open(QIODevice::WriteOnly); // 打开缓冲区以进行写入
    // 将图片保存到缓冲区中（以PNG格式）
    image.save(&buffer, "PNG");

    // 发送图片数据到服务器
    m_tcpSock->write(imageData);

}

void Shop_1::on_bt_3_clicked()
{

}

void Shop_1::on_bt_4_clicked()//打开摄像头
{
    m_sxt->show();
}

void Shop_1::on_bt_6_clicked()
{
    m_tcpSock->close();
    ui->bt_7->setEnabled(true);
    int i = 1;
    emit sendInt(i);
    this->close();

}

void Shop_1::on_bt_7_clicked()//连接tcp
{
    QString ip = "127.0.0.1";
    m_tcpSock->connectToHost(ip, 8899);
}
